﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WX.WMS.Common
{
    /// <summary>
    /// DataTypeConvert Description。
    /// </summary>
    public class DataTypeConvert
    {
        #region ToLong

        /// <summary>
        /// change the object to the integer
        /// Range:-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
        /// Size：64bit
        /// .Net FrameWork Type：System.Int64
        /// </summary>
        /// <param name="o">object value</param>
        /// <returns>Long value</returns>
        public static long ToLong(object o)
        {
            if (o == null || o == DBNull.Value)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToInt64(o);
                }
                catch
                {
                    return 0;
                }
            }
        }

        #endregion

        #region ToInt

        /// <summary>
        /// change the object to Int
        /// Range: -2,147,483,648 to 2,147,483,647
        /// Size:32 
        /// .Net FrameWork Type: System.Int64 
        /// </summary>
        /// <param name="o">object value</param>
        /// <returns>Int value</returns>
        public static int ToInt(object o)
        {
            if (o == null || o == DBNull.Value)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToInt32(o);
                }
                catch
                {
                    return 0;
                }
            }
        }

        #endregion

        #region ToDouble

        /// <summary>
        /// change the object to double
        /// Range: ±5.0E-324 to ±1.7E+308
        /// Size: 15 to 16
        /// .Net FrameWork type: System.Double
        /// </summary>
        /// <param name="o">object</param>
        /// <returns>double value</returns>
        public static double ToDouble(object o)
        {
            if (o == null || o == DBNull.Value)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToDouble(o);
                }
                catch
                {
                    return 0;
                }
            }
        }

        #endregion

        #region ToDecimal

        /// <summary>
        /// change the object to decimal
        /// Range：±1.0E-28 to ±7.9E+28
        /// Size: 28 to 29
        /// .Net FrameWork type: System.Decimal
        /// </summary>
        /// <param name="o">object</param>
        /// <returns>decimal value</returns>
        public static decimal ToDecimal(object o)
        {
            if (o == null || o == DBNull.Value)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToDecimal(o);
                }
                catch
                {
                    return 0;
                }
            }
        }

        #endregion

        #region ToString

        /// <summary>
        /// change object to String
        /// .Net FrameWork type: System.String
        /// </summary>
        /// <param name="o">object</param>
        /// <returns>string</returns>
        public static string ToString(object o)
        {
            if (o == null || o == DBNull.Value)
            {
                return string.Empty;
            }
            else
            {
                try
                {
                    return Convert.ToString(o);
                }
                catch
                {
                    return string.Empty;
                }
            }
        }

        #endregion

        #region ToDateTime

        /// <summary>
        /// change the object to DateTime
        /// </summary>
        /// <param name="o">object</param>
        /// <returns>DateTime</returns>
        public static DateTime ToDateTime(object o)
        {
            if (o == null || o == System.DBNull.Value)
            {
                return new DateTime(1900, 1, 1);
            }
            else
            {
                try
                {
                    return Convert.ToDateTime(o);
                }
                catch
                {
                    return new DateTime(1900, 1, 1);
                }
            }
        }

        #endregion

        #region ToNull

        /// <summary>
        /// change the object to null
        /// </summary>
        /// <param name="o">object</param>
        /// <returns>null</returns>
        public static string ToNull()
        {
            return null;
        }

        #endregion

        #region StrToInts
        /// <summary>
        /// change the string to int[]
        /// </summary>
        /// <param name="strValue">string value</param>
        /// <param name="token">splitting symbol</param>
        /// <returns>int[]</returns>
        public static int[] StrToInts(string strValue, char token)
        {
            int[] ret = null;
            if (strValue != null)
            {
                string[] tempstr = strValue.Split(token);
                ret = new int[tempstr.Length];
                for (int i = 0; i < tempstr.Length; i++)
                {
                    ret[i] = ToInt(tempstr[i]);
                }
            }
            return ret;
        }

        #endregion

        #region StrToStrs
        /// <summary>
        /// change the string to string[]
        /// </summary>
        /// <param name="strValue">string value</param>
        /// <param name="token">splitting symbol</param>
        /// <returns>string[]</returns>
        public static string[] StrToStrs(string strValue, char token)
        {
            string[] ret = null;
            if (strValue != null)
            {
                string[] tempstr = strValue.Split(token);
                ret = new string[tempstr.Length];
                for (int i = 0; i < tempstr.Length; i++)
                {
                    ret[i] = tempstr[i];
                }
            }
            return ret;
        }

        #endregion

        #region StrToLongs

        /// <summary>
        /// change the string to long[]
        /// </summary>
        /// <param name="strValue">string value</param>
        /// <param name="token">splitting symbol</param>
        /// <returns>long[]</returns>
        public static long[] StrToLongs(string strValue, char token)
        {
            long[] ret = null;
            if (strValue != null)
            {
                string[] tempstr = strValue.Split(token);
                ret = new long[tempstr.Length];
                for (int i = 0; i < tempstr.Length; i++)
                {
                    ret[i] = ToLong(tempstr[i]);
                }
            }
            return ret;
        }

        #endregion

        #region DateToInt

        /// <summary>
        /// change the datetime to the int
        /// </summary>
        /// <param name="dt">datetime value</param>
        /// <returns>int</returns>
        public static int DateToInt(DateTime dt)
        {
            return ToInt(DateToStr(dt));
        }

        #endregion

        #region DateToStr

        /// <summary>
        /// change the datetime to the string
        /// </summary>
        /// <param name="dt">datetime value</param>
        /// <returns>string</returns>
        public static string DateToStr(DateTime dt)
        {
            string[] str = dt.ToShortDateString().Split('-');

            return str[0] + GetString(str[1]) + GetString(str[1]);
        }

        #endregion

        #region GetString
        /// <summary>
        /// convert one bit number to double 1->01
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetString(string str)
        {
            string m = "";
            switch (str)
            {
                case "0":
                    m = "00";
                    break;
                case "1":
                    m = "01";
                    break;
                case "2":
                    m = "02";
                    break;
                case "3":
                    m = "03";
                    break;
                case "4":
                    m = "04";
                    break;
                case "5":
                    m = "05";
                    break;
                case "6":
                    m = "06";
                    break;
                case "7":
                    m = "07";
                    break;
                case "8":
                    m = "08";
                    break;
                case "9":
                    m = "09";
                    break;
                default:
                    m = str;
                    break;
            }
            return m;
        }

        #endregion

        #region FirstCharUpper

        /// <summary>
        /// change the first letter of the string to the upper
        /// </summary>
        /// <param name="strValue">string value</param>
        /// <returns>string</returns>
        public static string FirstCharUpper(string strValue)
        {
            if (strValue != null)
            {
                if (strValue.Length >= 2)
                {
                    return strValue.Substring(0, 1).ToUpper() + strValue.Substring(1, strValue.Length - 1);
                }
                else
                {
                    return strValue;
                }
            }
            else
            {
                return "";
            }
        }

        #endregion

        #region FirstCharLower

        /// <summary>
        /// change the first letter of the string to the lower
        /// </summary>
        /// <param name="strValue">string value</param>
        /// <returns>string</returns>
        public static string FirstCharLower(string strValue)
        {
            if (strValue != null)
            {
                if (strValue.Length >= 2)
                {
                    return strValue.Substring(0, 1).ToLower() + strValue.Substring(1, strValue.Length - 1);
                }
                else
                {
                    return strValue;
                }
            }
            else
            {
                return "";
            }
        }

        #endregion

        #region ConvertWeekDayToDate

        /// <summary>
        /// Get the datetime from which week in the year and which day in the week
        /// </summary>
        /// <param name="year"></param>
        /// <param name="weekofyear"></param>
        /// <param name="dayofweek"></param>
        /// <param name="hour"></param>
        /// <returns></returns>
        public static DateTime ConvertWeekDayToDate(int year, int weekofyear, int dayofweek, int hour)
        {
            return ConvertWeekDayToDate(year, weekofyear, dayofweek, hour, 0, 0);
        }

        /// <summary>
        /// Get the datetime from which week in the year and which day in the week
        /// </summary>
        /// <param name="year"></param>
        /// <param name="weekofyear"></param>
        /// <param name="dayofweek"></param>
        /// <param name="hour"></param>
        /// <param name="minute"></param>
        /// <returns></returns>
        public static DateTime ConvertWeekDayToDate(int year, int weekofyear, int dayofweek, int hour, int minute)
        {
            return ConvertWeekDayToDate(year, weekofyear, dayofweek, hour, minute, 0);
        }

        /// <summary>
        /// Get the datetime from which week in the year and which day in the week
        /// </summary>
        /// <param name="year"></param>
        /// <param name="weekofyear"></param>
        /// <param name="dayofweek"></param>
        /// <param name="hour"></param>
        /// <param name="minute"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        public static DateTime ConvertWeekDayToDate(int year, int weekofyear, int dayofweek, int hour, int minute, int second)
        {
            DateTime yearD = new DateTime(year, 1, 1);
            int Week = Convert.ToInt32(yearD.DayOfWeek);
            if (Week == 0)
            {
                Week = 7;
            }

            int OutDay = weekofyear * 7 - (Week - 1);//抛去多余天数,到这周的礼拜天
            int sum = OutDay;//该周已经过了多少天，周日为最后一天

            try
            {
                if (sum < 7)//First Week
                {
                    if (dayofweek < Week)
                        yearD = new DateTime(year - 1, 12, 31 - ((7 - dayofweek) - sum));
                    else
                        yearD = new DateTime(year, 1, sum - (7 - dayofweek));
                }
                else
                {
                    int yearnow = 0;
                    int monthnow = 0;
                    for (int month = 1; month <= 13; month++)
                    {
                        int nextMonth = 0;
                        int daysInJuly = 0;
                        if (month >= 12)
                        {
                            if (month == 13)
                            {
                                yearnow = year + 1;
                                monthnow = 1;
                                nextMonth = System.DateTime.DaysInMonth(yearnow, monthnow + 1);
                                daysInJuly = System.DateTime.DaysInMonth(yearnow, monthnow);
                            }
                            else
                            {
                                nextMonth = System.DateTime.DaysInMonth(year + 1, 1);
                                daysInJuly = System.DateTime.DaysInMonth(year, month);
                            }
                        }
                        else
                        {
                            nextMonth = System.DateTime.DaysInMonth(year, month + 1);//下一个月
                            daysInJuly = System.DateTime.DaysInMonth(year, month);//当月天数
                        }

                        if ((sum - daysInJuly) < 0)
                        {
                            if (sum <= 7)
                            {
                                if (yearnow == year + 1)
                                {
                                    //next year
                                    yearD = new DateTime(yearnow, monthnow, sum - (7 - dayofweek), hour, minute, second);
                                }
                                else
                                {
                                    int lastMonth = System.DateTime.DaysInMonth(year, month - 1);//上一个月
                                    yearD = new DateTime(year, month - 1, lastMonth - (7 - dayofweek), hour, minute, second);
                                }
                            }
                            else
                                if (sum < daysInJuly)
                                {
                                    yearD = new DateTime(year, month, sum - (7 - dayofweek), hour, minute, second);
                                }
                            break;
                        }
                        else
                            if ((sum - daysInJuly) < 0 && (sum - daysInJuly - nextMonth) < dayofweek)
                            {
                                dayofweek = sum - daysInJuly;
                                yearD = new DateTime(year, month, daysInJuly - dayofweek, hour, minute, second);
                                break;
                            }
                        sum = sum - daysInJuly;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return yearD;
        }

        #endregion
    }
}
